package com.huayi.website.controller.manage;

import com.huayi.website.entity.PageContentEntity;
import com.huayi.website.entity.PhotoEntity;
import com.huayi.website.entity.ProductEntity;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.PageContentService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.PagingResponse;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MPageContentController {
@Autowired
private PageContentService pageContentService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/page_content_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {
        var result = pageContentService.getAllList(null);

        model.addAttribute("list", result);
        model.addAttribute("totalSize", result.size());


        model.addAttribute("menu","page_content");
        return "manage/page_content_list";
    }

    /**
     * 添加
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/page_content_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {


        model.addAttribute("pageContent",new PageContentEntity());
        model.addAttribute("menu","page_content");
        return "manage/page_content_add";
    }

    @RequestMapping(value = "/page_content_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var pc = pageContentService.getById(id);

        model.addAttribute("pageContent",pc);
        model.addAttribute("menu","page_content");
        return "manage/page_content_add";
    }

    @ResponseBody
    @RequestMapping(value = "/page_content_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        String title = request.getParameter("title");
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)) id="0";
        String content = request.getParameter("content");
        String sort = request.getParameter("sort");
        //图片
        String images = request.getParameter ("image");
        String image1 = request.getParameter ("image1");
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));
        Integer language = Integer.parseInt(request.getParameter("language"));



        Integer pcId = pageContentService.editPageContent(Integer.parseInt(id),title,images,image1,content,Integer.parseInt(sort),pageId,language);

        return new ResultVo<>(0, "SUCCESS");
    }

    /**
     * 删除
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/page_content_delete", method = RequestMethod.POST)
    public ResultVo<Integer> delete(@RequestBody GoodsDeleteReq req) {
        if (req.getId() == null || req.getId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError.getIndex(), "参数错误，缺少id");

        pageContentService.delete(req.getId());

        return new ResultVo<>(EnumResultVo.SUCCESS);
    }

}
